<?php

namespace System\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use System\Model\Link;

class LinkTable extends AbstractTableGateway {

    protected $table = "tblurl";
    protected $sql;

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Link());
        $this->initialize();
        $this->sql = new Sql($this->adapter);
    }

    public function loadlink($urlloai) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->where(array("urlloadi" => $urlloai));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $result = $pst->execute();
        $data = array();
        foreach ($result as $results) {
            $data[] = $results;
        }
        return $data;
    }

    public function addlink(Link $link) {
        $data = $link->getArray();
        $sqlEx = $this->sql->insert();
        $sqlEx->into($this->table);
        $sqlEx->values($data);
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        if ($rs != null) {
            return true;
        } else {
            return false;
        }
    }

    public function activemodel($linkId, $active) {
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set(array("urlActive" => $active));
        $sqlEx->where(array("urlId" => $linkId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        if ($rs != null) {
            return true;
        } else {
            return false;
        }
    }

    public function updatelink($data, $linkId) {

       
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set($data);
        $sqlEx->where(array("urlId" => $linkId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        if ($rs != null) {
            return true;
        } else {
            return false;
        }
    }

    public function dellink($linkId) {
        $sqlEx = $this->sql->delete();
        $sqlEx->from($this->table);
        $sqlEx->where(array("urlId" => $linkId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        if ($rs != null) {
            return true;
        } else {
            return false;
        }
    }

    public function loadedit($linkId) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->where(array("urlId" => $linkId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $result = $pst->execute();

        $data = $result->current();
        return $data;
    }

}
